<?php

namespace app\api\model;

use think\Db;
use think\Model;


class Banner extends Model
{
    // 表名
    protected $name = 'banner';

    // 追加属性
    protected $append = [
        'position_text',
        'type_text',
        'object',
        'page',
    ];

    public function getImageAttr($value){
        return cdnurl($value,true);
    }
    
    public function getPositionList()
    {
        return ['1' => __('Position 1')];
    }

    public function getTypeList()
    {
//        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3'), '4' => __('Type 4')];
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }

    public function getPositionTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['position']) ? $data['position'] : '');
        $list = $this->getPositionList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getObjectAttr($value,$data){
        switch ($data['type']){
            case 1:
                $value=Db::name('shop_goods')->where(['id'=>$data['object_id']])->value('name');
                break;
            default:
                $value='';
                break;
        }
        return $value;
    }

    public function getPageAttr($value,$data){
        switch ($data['type']){
            case 1:
                $value='/pages/index/active/active?id='.$data['object_id'];
                break;
            case 2:
                $value='/pages/index/goodsDetail/goodsDetail?id='.$data['object_id'];
                break;
            case 3:
                $value='/pages/person/coupon/coupon';
                break;
            case 4:
                $value='/pages/person/shopVoucher/shopVoucher';
                break;
            default:
                $value='';
                break;
        }
        return $value;
    }


}
